Skip to content

CSHARP-5633: BsonSerializer.IsTypeDiscriminated is not thread-safe #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2025

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Jul 10, 2025

@papafe papafe requested a review from a team as a code owner July 10, 2025 15:47
@papafe papafe added the bug label Jul 10, 2025
@papafe papafe requested review from ajcvickers and removed request for a team July 10, 2025 15:47
@BorisDog BorisDog requested a review from sanych-sun July 10, 2025 16:27
return true;
}

__configLock.EnterReadLock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please double-check if introducing this synchronization affects performance. If so, we could replace HashSet<Type> with ConcurrentDictionary<Type, bool>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to use a ConcurrentDictionary, thanks for taking time to do the benchmarks :)

@papafe papafe requested review from sanych-sun and rstam July 15, 2025 09:16
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Two small comments.

@@ -321,8 +321,7 @@ internal static bool IsDiscriminatorConventionRegisteredAtThisLevel(Type type)
/// <returns>True if the type is discriminated.</returns>
public static bool IsTypeDiscriminated(Type type)
{
var typeInfo = type.GetTypeInfo();
return typeInfo.IsInterface || __discriminatedTypes.Contains(type);
return type.GetTypeInfo().IsInterface || __discriminatedTypes.ContainsKey(type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetTypeInfo is left over from when we used to target a very old .NET Core framework.

I think this line can be just:

return type.IsInterface || __discriminatedTypes.ContainsKey(type);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

@@ -587,7 +586,7 @@ public static void RegisterDiscriminator(Type type, BsonValue discriminator)
// mark all base types as discriminated (so we know that it's worth reading a discriminator)
for (var baseType = typeInfo.BaseType; baseType != null; baseType = baseType.GetTypeInfo().BaseType)
{
__discriminatedTypes.Add(baseType);
__discriminatedTypes.TryAdd(baseType, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK that we are ignoring the return value of TryAdd?

I understand that we expect it to always return true given the check on line 582.

Should we document that we are ignoring the return value on purpose by coding like this:

_ = __discriminatedTypes.TryAdd(baseType, true);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe to ignore the return value of TryAdd in the same way we ignored Add before (the return value has the same semantic).
Maybe adding a comment would make it clearer, I'm not a super fan of adding the discard operator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... I am a fan of the discard variable. I always prefer code to comments to document something.

But it's fine either way.

Copy link
Member

@sanych-sun sanych-sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@papafe papafe requested a review from rstam July 15, 2025 17:15
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -587,7 +586,7 @@ public static void RegisterDiscriminator(Type type, BsonValue discriminator)
// mark all base types as discriminated (so we know that it's worth reading a discriminator)
for (var baseType = typeInfo.BaseType; baseType != null; baseType = baseType.GetTypeInfo().BaseType)
{
__discriminatedTypes.Add(baseType);
__discriminatedTypes.TryAdd(baseType, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... I am a fan of the discard variable. I always prefer code to comments to document something.

But it's fine either way.

@papafe papafe merged commit d2802b6 into mongodb:main Jul 16, 2025
30 of 35 checks passed
@papafe papafe deleted the csharp5633 branch July 16, 2025 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants